package com.neuedu.mblog.controller;

import com.alibaba.fastjson.JSON;
import com.neuedu.mblog.utils.BlogException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.SQLException;

/**
 * 项目：his
 * 创建时间：  2021/12/10   9:00
 * 作者 :jshand
 * 描述 :
 */
public abstract class BaseController extends HttpServlet {
    /**
     * 主控的函数
     * - list :查询列表
     * - delete ：删除
     * - toAdd： 跳转添加页面
     * - save：保存添加的信心
     * - toEdit:跳转到修改页
     * - update:更新
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        try{
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");

            //请求类型
            String _type = request.getParameter("_type");
            if(_type == null || "".equals(_type)){
                _type = "list";
            }
            //如果不是普通的增删改查的 controller，需要根据  _type的值 访问不同的方法
            Class objClass = this.getClass();

            Method typeMethod = objClass.getDeclaredMethod(_type, HttpServletRequest.class, HttpServletResponse.class);
            if(typeMethod == null) {
                request.getRequestDispatcher("/error/404.jsp").forward(request,response);
                return;
            }

            //调用obj 的 method方法
            //public  可以直接访问，
            //设置让方法可以被调用（非public）
            typeMethod.setAccessible(true);

            typeMethod.invoke(this, request, response);


        }catch (Exception e){
            e.printStackTrace();
            request.getRequestDispatcher("/error/500.jsp").forward(request,response);
        }
    }

    /**
     * 用于查询
     *
     * @param request
     * @param response
     */
    protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException, ClassNotFoundException {

        throw  new ServletException("Controller【"+getClass().getName()+"】 子类没有实现list方法");
    }
    /**
     * 用于ajaxDelete
     *
     * @param request
     * @param response
     */
    protected void ajaxDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {

        throw  new ServletException("Controller【"+getClass().getName()+"】 子类没有实现ajaxDelete方法");
    }

    /**
     * 根据主键删除
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        throw  new ServletException("Controller【"+getClass().getName()+"】 子类没有实现delete方法");
    }

    /**
     * 跳转到添加页面
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        throw  new ServletException("Controller【"+getClass().getName()+"】 子类没有实现toAdd方法");
    }

    /**
     * 保存新增的刷剧
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        throw  new ServletException("Controller【"+getClass().getName()+"】 子类没有实现save方法");
    }

    /**
     * 跳转到修改页面
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        throw  new ServletException("Controller【"+getClass().getName()+"】 子类没有实现toEdit方法");
    }

    /**
     * 更新数据
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        throw  new ServletException("Controller【"+getClass().getName()+"】 子类没有实现update方法");
    }



    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    /**
     * 统一的ajax 返回结果
     * @param response
     * @param success
     */
    protected void jsonResult(HttpServletResponse response, boolean success) throws IOException {

        String json = "{\"success\":"+success+"}";
        PrintWriter out = response.getWriter();
        out.write(json);
        out.close();
    }


    protected void jsonResult(HttpServletResponse response, Object obj) throws IOException {

        String json = JSON.toJSONString(obj);
        PrintWriter out = response.getWriter();
        out.write(json);
        out.close();
    }
}
